/**
 * Copyright (c) 2013 Axa Holding Belgium, SA. All rights reserved.
 * This software is the confidential and proprietary information of the AXA Group.
 */
package chabernac.gravidroid.model;

public class Vector {
	public float x = 0;
	public float y = 0;

	public Vector(Vector aP1, Vector aP2) {
		x = aP2.x - aP1.x;
		y = aP2.y - aP1.y;
	}

	public Vector() {
	}

	public Vector(float x, float y) {
		this.x = x;
		this.y = y;
	}

	public Vector(Vector aVector) {
		x = aVector.x;
		y = aVector.y;
	}

	public float getLength() {
		return (float) Math.sqrt(x * x + y * y);
	}

	public void add(Vector aVector, Vector aDestination) {
		aDestination.x = x + aVector.x;
		aDestination.y = y + aVector.y;
	}

	public void minus(Vector aVector, Vector aDestination) {
		aDestination.x = x - aVector.x;
		aDestination.y = y - aVector.y;
	}

	public float getQuadraticLength() {
		return x * x + y * y;
	}

	public void normalize(Vector aDestination) {
		float theLength = getLength();
		if (theLength == 0)
			return;
		aDestination.x = x / theLength;
		aDestination.y = y / theLength;
	}

	public void multiply(float aFactor, Vector aDestination) {
		aDestination.x = x * aFactor;
		aDestination.y = y * aFactor;
	}

	public void invertX(Vector aDestination) {
		aDestination.x = -x;
	}

	public void invertY(Vector aDestination) {
		aDestination.y = -y;
	}

	public void invert(Vector aDestination) {
		aDestination.x = -x;
		aDestination.y = -y;
	}

	@Override
	public String toString() {
		return "x: " + x + " y: " + y;
	}

	public void set(float x, float y) {
		this.x = x;
		this.y = y;
	}

	public void set(Vector aVector) {
		x = aVector.x;
		y = aVector.y;
	}

	public void divide(float aFactor, Vector aDestination) {
		aDestination.x = x / aFactor;
		aDestination.y = y / aFactor;
	}

	public float dotProduct(Vector aVector) {
		return x * aVector.x + y * aVector.y;
	}

	@Override
	public boolean equals(Object aVector) {
		if (!(aVector instanceof Vector))
			return false;
		Vector theVector = (Vector) aVector;
		if (Math.abs(x - theVector.x) > 0.00001)
			return false;
		if (Math.abs(y - theVector.y) > 0.00001)
			return false;
		return true;
	}

	public float getAngle(Vector aVector) {
		return (float) Math.acos(dotProduct(aVector) / (getLength() * aVector.getLength()));
	}
}
